package 链表与双指针.m03.d06.t04;

import 链表与双指针.ListNode;

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode reverseBetween(ListNode head, int left, int right) {
        ListNode cur = head, LNode = null, RNode = null;
        while (cur.next != null) {
            if (cur.next.val == left) {
                LNode = cur.next;
                break;
            }
            cur = cur.next;
        }
        if(LNode == null) {
            return head;
        }
        ListNode prev = cur, pprev = cur;
        cur = cur.next;
        ListNode tmp = cur, next = cur.next;
        while (prev != null && prev.val != right) {
            next = cur.next;
            cur.next = prev;
            prev = cur;
            cur = next;
        }
        pprev.next = prev;
        tmp.next = next;
        return head;
    }
}